package com.sany.taskmanager.ui.widget;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.sany.taskmanager.R;
import com.sany.taskmanager.databinding.DialogMaterialNumBinding;
import com.sany.taskmanager.global.Global;
import com.sany.taskmanager.global.Key;
import com.sany.taskmanager.util.EditUtil;
import com.sany.taskmanager.util.LogUtil;
import com.sany.taskmanager.util.PreferenceUtil;
import com.sany.taskmanager.util.ToastUtil;


/**
 * 本地IP地址弹框
 */
public class IPDialog extends CommonDialog<DialogMaterialNumBinding> {

    public IPDialog(@NonNull Context context) {
        super(context, context.getString(R.string.title_ip_input));
    }

    @Override
    protected void initData() {
        vBinding.etMaterialNum.setTransformationMethod(EditUtil.replacementTransformationMethod);
        String ip = PreferenceUtil.getPreferences().getString(Key.SharedKey.IP);
        LogUtil.d("ip = " + ip);
        if (TextUtils.isEmpty(ip)) {
            ip = Global.IP_ADDRESS;
        }
        vBinding.etMaterialNum.setText(ip);
    }

    @Override
    protected boolean confirm() {
        String ip = vBinding.etMaterialNum.getText().toString();
        PreferenceUtil.getPreferences().putString(Key.SharedKey.IP, ip);
        ToastUtil.showToast(getContext(), "IP地址修改成功！");
        vBinding.etMaterialNum.postDelayed(new Runnable() {
            @Override
            public void run() {
                System.exit(0);
            }
        }, 1500);
        return true;
    }


}
